﻿Imports Microsoft.VisualBasic
Imports System.Data.SqlClient


Namespace HensonOnline
    Public Class Events

        Private _EventId As Integer
        Private _UserId As Integer
        Private _Title As String
        Private _EventDate As Date
        Private _Location As String
        Private _Archive As Boolean
        Private _IsPrivate As Boolean

        Public Property EventId() As Integer
            Get
                Return _EventId
            End Get
            Set(ByVal value As Integer)
                _EventId = value
            End Set
        End Property
        Public Property UserId() As Integer
            Get
                Return _UserId
            End Get
            Set(ByVal value As Integer)
                _UserId = value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
        Public Property EventDate() As DateTime
            Get
                Return _EventDate
            End Get
            Set(ByVal value As DateTime)
                _EventDate = value
            End Set
        End Property
        Public Property Location() As String
            Get
                Return _Location
            End Get
            Set(ByVal value As String)
                _Location = value
            End Set
        End Property
        Public Property Archive() As Boolean
            Get
                Return _Archive
            End Get
            Set(ByVal value As Boolean)
                _Archive = value
            End Set
        End Property
        Public Property IsPrivate() As Boolean
            Get
                Return _IsPrivate
            End Get
            Set(ByVal value As Boolean)
                _IsPrivate = value
            End Set
        End Property

        Private Sub PopulateDefault()
            _EventId = 0
            _UserId = 0
            _Title = ""
            _EventDate = CDate("1/1/1900")
            _Location = ""
            _Archive = False
            _IsPrivate = False
        End Sub

        Public Sub New()
            PopulateDefault()
        End Sub

        Public Sub New(ByVal eventId As Integer)
            GetEvent(eventId)
        End Sub


        Protected Function GetEvent(ByVal eventId As Integer) As ArrayList
            Dim sql As String = "SELECT EventId, UserId, Title, Date, Location, Archive, Private FROM Events WHERE EventId = @EventId"
            Dim type As System.Data.CommandType = System.Data.CommandType.Text
            Dim parameters As List(Of System.Data.SqlClient.SqlParameter) = New List(Of System.Data.SqlClient.SqlParameter)
            parameters.Add(New System.Data.SqlClient.SqlParameter("@EventId", eventId))
            Dim data As New DataAccess
            Dim propertyList As ArrayList = data.SelectSingle(sql, type, parameters.ToArray, 7)

            If propertyList.Count > 0 Then
                _EventId = propertyList(0)
                _UserId = propertyList(1)
                _Title = propertyList(2)
                _EventDate = propertyList(3)
                _Location = propertyList(4)
                _Archive = propertyList(5)
                _IsPrivate = propertyList(6)
            Else
                _IsPrivate = True
            End If

            Return propertyList
        End Function

    End Class

    Public Class EventDetails
        Inherits Events

        Private _AlbumId As Integer
        Private _Desc As String
        Private _Address As String
        Private _City As String
        Private _State As String
        Private _Zip As String

        Public Property AlbumId() As Integer
            Get
                Return _AlbumId
            End Get
            Set(ByVal value As Integer)
                _AlbumId = value
            End Set
        End Property
        Public Property Desc() As String
            Get
                Return _Desc
            End Get
            Set(ByVal value As String)
                _Desc = value
            End Set
        End Property
        Public Property Address() As String
            Get
                Return _Address
            End Get
            Set(ByVal value As String)
                _Address = value
            End Set
        End Property
        Public Property City() As String
            Get
                Return _City
            End Get
            Set(ByVal value As String)
                _City = value
            End Set
        End Property
        Public Property State() As String
            Get
                Return _State
            End Get
            Set(ByVal value As String)
                _State = value
            End Set
        End Property
        Public Property Zip() As String
            Get
                Return _Zip
            End Get
            Set(ByVal value As String)
                _Zip = value
            End Set
        End Property


        Private Sub PopulateDefault()
            _Desc = ""
            _Address = ""
            _City = ""
            _State = ""
            _Zip = ""
        End Sub

        Public Sub New()
            PopulateDefault()
        End Sub

        Public Sub New(ByVal eventId As Integer)
            MyBase.New(eventId)
            GetEventDetails(eventId)
        End Sub

        Public Function Create(ByVal hasAlbum As Boolean) As Integer
            Dim connString As String = ConfigurationManager.ConnectionStrings("Henson Family").ConnectionString
            Dim conn As SqlConnection = New SqlConnection(connString)
            Dim comm As SqlCommand = New SqlCommand()

            conn.Open()

            comm.Connection = conn
            comm.CommandText = "sp_createEvent"
            comm.CommandType = Data.CommandType.StoredProcedure

            comm.Parameters.AddWithValue("@UserId", UserId)
            comm.Parameters.AddWithValue("@AlbumId", 0)
            comm.Parameters.AddWithValue("@Title", Title)
            comm.Parameters.AddWithValue("@Desc", Desc)
            comm.Parameters.AddWithValue("@Date", EventDate)
            comm.Parameters.AddWithValue("@Location", Location)
            comm.Parameters.AddWithValue("@Address", Address)
            comm.Parameters.AddWithValue("@City", City)
            comm.Parameters.AddWithValue("@State", State)
            comm.Parameters.AddWithValue("@Zip", Zip)
            comm.Parameters.AddWithValue("@Archive", Archive)
            comm.Parameters.AddWithValue("@Private", IsPrivate)
            comm.Parameters.AddWithValue("@HasAlbum", hasAlbum)
            comm.ExecuteNonQuery()
            comm.Parameters.Clear()

            EventId = CInt(comm.ExecuteScalar())

            conn.Close()

            Return EventId
        End Function



        Public Function GetEventDetails(ByVal eventId As Integer) As ArrayList
            Dim sql As String = "SELECT EventId, UserId, AlbumId, Title, [Desc], Date, Location, Address, City, State, Zip, Archive, Private FROM Events WHERE EventId = @EventId"
            Dim type As System.Data.CommandType = System.Data.CommandType.Text
            Dim parameters As List(Of System.Data.SqlClient.SqlParameter) = New List(Of System.Data.SqlClient.SqlParameter)
            parameters.Add(New System.Data.SqlClient.SqlParameter("@EventId", eventId))
            Dim data As New DataAccess
            Dim propertyList As ArrayList = data.SelectSingle(sql, type, parameters.ToArray, 13)

            If propertyList.Count > 0 Then
                MyBase.EventId = propertyList(0)
                UserId = propertyList(1)
                AlbumId = propertyList(2)
                'Title = propertyList(3)
                Desc = propertyList(4)
                EventDate = propertyList(5)
                Location = propertyList(6)
                Address = propertyList(7)
                City = propertyList(8)
                State = propertyList(9)
                Zip = propertyList(10)
                Archive = propertyList(11)
                IsPrivate = propertyList(12)
            Else
                IsPrivate = True
            End If

            Return propertyList
        End Function

    End Class
End Namespace
